package cn.anecansaitin.jep.interpreter;

import cn.anecansaitin.jep.interpreter.evaluators.*;
import cn.anecansaitin.jep.parser.ast.node.Node;
import cn.anecansaitin.jep.parser.ast.node.expression.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class Interpreter {
    @Nullable
    public Object evaluate(@NotNull Expression expression) throws RuntimeException {
        return switch (expression.resultType()) {
            case INT -> IntEvaluator.evaluate(expression);
            case LONG -> LongEvaluator.evaluate(expression);
            case FLOAT -> FloatEvaluator.evaluate(expression);
            case DOUBLE -> DoubleEvaluator.evaluate(expression);
            case BOOLEAN -> BooleanEvaluator.evaluate(expression);
            case CHAR -> CharEvaluator.evaluate(expression);
            case STRING -> StringEvaluator.evaluate(expression);
            case null, default -> throw new RuntimeException("未实现的类型");
        };
    }

    public static RuntimeException markError(Node node, String message) {
        return new RuntimeException("[" + node.line() + ":" + node.column() + "] " + message);
    }
}